package com.aw.baselib.net.callback;

import com.aw.baselib.base.AwBaseView;

/**
 * Created by hzw.
 */

public abstract class AwApiCallback<T> implements ApiBaseCallback<T>{

    private AwBaseView mBaseView;
    private boolean needLoading = true;
    private boolean needCallbackFail = false;

    public AwApiCallback(AwBaseView baseView) {
        mBaseView = baseView;
    }

    public AwApiCallback(AwBaseView baseView, boolean needCallbackFail) {
        mBaseView = baseView;
        this.needCallbackFail = needCallbackFail;
    }

    public AwApiCallback(AwBaseView baseView, boolean needLoading, boolean needCallbackFail) {
        mBaseView = baseView;
        this.needLoading = needLoading;
        this.needCallbackFail = needCallbackFail;
    }

    @Override
    public void onStart() {
        if(needLoading && mBaseView != null) {
            mBaseView.showLoadingDialog();
        }
    }

    public abstract void onSuccess(T result);

    @Override
    public void onFail(int code, String msg) {
        if(!needCallbackFail && mBaseView != null) {
            mBaseView.showMsg(msg);
        }
        onCompleted();
    }

    @Override
    public void onCompleted() {
        if(needLoading && mBaseView != null) {
            mBaseView.dismissLoadingDialog();
        }
    }

}
